package com.hu.coupon.project.service.impl;

import com.hu.coupon.project.entity.SmsMemberPriceEntity;
import com.hu.coupon.project.entity.SmsSkuLadderEntity;
import com.hu.coupon.project.service.SmsMemberPriceService;
import com.hu.coupon.project.service.SmsSkuLadderService;
import com.hu.gulimallcommon.to.MemberPrice;
import com.hu.gulimallcommon.to.SkuProductTo;
import com.hu.gulimallcommon.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.gulimallcommon.utils.Query;

import com.hu.coupon.project.dao.SmsSkuFullReductionDao;
import com.hu.coupon.project.entity.SmsSkuFullReductionEntity;
import com.hu.coupon.project.service.SmsSkuFullReductionService;


@Service("smsSkuFullReductionService")
public class SmsSkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionDao, SmsSkuFullReductionEntity> implements SmsSkuFullReductionService {
    @Autowired
    SmsSkuLadderService skuLadderService;
    @Autowired
    SmsMemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSkuFullReductionEntity> page = this.page(
                new Query<SmsSkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SmsSkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(SkuProductTo skuProductTo) {
        //1。保存阶梯价格
        SmsSkuLadderEntity smsSkuLadder = new SmsSkuLadderEntity();
        smsSkuLadder.setSkuId(skuProductTo.getSkuId());
        smsSkuLadder.setDiscount(skuProductTo.getDiscount());
        smsSkuLadder.setFullCount(skuProductTo.getFullCount());
        smsSkuLadder.setAddOther(skuProductTo.getCountStatus());
        skuLadderService.save(smsSkuLadder);
        //2.保存满减信息
        SmsSkuFullReductionEntity smsSkuFullReduction = new SmsSkuFullReductionEntity();
        BeanUtils.copyProperties(skuProductTo,smsSkuFullReduction);
        this.save(smsSkuFullReduction);
        //3.保存会员价格
        List<MemberPrice> memberPrice = skuProductTo.getMemberPrice();
        List<SmsMemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
            SmsMemberPriceEntity smsMemberPrice = new SmsMemberPriceEntity();
            smsMemberPrice.setSkuId(skuProductTo.getSkuId());
            smsMemberPrice.setMemberPrice(item.getPrice());
            smsMemberPrice.setMemberLevelId(item.getId());
            smsMemberPrice.setMemberLevelName(item.getName());
            smsMemberPrice.setAddOther(1);
            return smsMemberPrice;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}